import React from 'react'
import { Route, Switch } from 'react-router-dom'  
import axios from 'axios'
import { Spin } from 'antd'

import Loading from './component/loading/loading'
import Login from './component/login/login'

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      hasError:false,
      spinning:false
    }
  }
  componentDidMount() {
    // 拦截请求
    axios.interceptors.request.use((config) => {
      this.setState({spinning:true})
      return config
    })
    
    // 拦截响应
    axios.interceptors.response.use((config) => {
      this.setState({spinning:false})
      return config
    })
  }
  componentDidCatch(err,info) {
    console.log(err,info)
    this.setState({
      hasError:true
    })
  }
  render() {
    return this.state.hasError ? 
      <h2>页面出错了</h2> :
      (
        <Spin spinning={this.state.spinning} delay={500}>
          <Switch>
            <Route path='/' component={Login}></Route>
          </Switch>
        </Spin>
        
      )
  }
}

export default App